Skip to content

Commit bd3d3fb

Browse files
vladimirradosavljevicakiramenai
authored andcommitted
[EraVM][EVM] Use std::string instead of Twine
Twine variables are prone to use-after-free bugs, as it is stated in the clang-tidy documentation: https://clang.llvm.org/extra/clang-tidy/checks/llvm/twine-local.html Signed-off-by: Vladimir Radosavljevic <[email protected]>
1 parent fe0f610 commit bd3d3fb

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

lld/lld-c/LLDAsLibraryC.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ createEraVMExeLinkerScript(uint64_t metadataSize,
224224
std::to_string(alignedMDSize), std::to_string(metadataSize))
225225
.str();
226226

227-
Twine scriptPart1 = Twine("\
227+
std::string scriptPart1 = "\
228228
ENTRY(0); \n\
229229
SECTIONS { \n\
230230
.code : SUBALIGN(1) { \n\
@@ -241,9 +241,9 @@ SECTIONS { \n\
241241
\n\
242242
ASSERT(. % 32 == 0, \"size isn't multiple of 32\"); \n\
243243
\n\
244-
/* Add padding before the metadata */\n");
244+
/* Add padding before the metadata */\n";
245245

246-
Twine scriptPart2 = Twine("\
246+
std::string scriptPart2 = "\
247247
*(.eravm-metadata) \n\
248248
\n\
249249
ASSERT(. % 64 == 32, \"size isn't odd number of words\"); \n\
@@ -256,9 +256,9 @@ SECTIONS { \n\
256256
is not needed. */ \n\
257257
/DISCARD/ : { \n\
258258
*(.data) \n\
259-
}}");
259+
}}";
260260

261-
return (linkerSymbolNamesStr + scriptPart1 + padding + scriptPart2).str();
261+
return linkerSymbolNamesStr + scriptPart1 + padding + scriptPart2;
262262
}
263263

264264
/// Performs linkage of the ELF object file passed in \p inBuffer, as
@@ -499,9 +499,11 @@ static std::string creteEVMLinkerScript(ArrayRef<LLVMMemoryBufferRef> memBufs,
499499
// __dataoffset_D_105_deployed = .;
500500
// D_105_deployed(.text);
501501
// __datasize_D_105_deployed = . - __dataoffset_D_105_deployed;
502-
Twine topLevel = topName + "(.text);\n" + dataOffsetPrefix + deployed +
503-
" = .;\n" + deployed + "(.text);\n" + dataSizePrefix +
504-
deployed + " = . - " + dataOffsetPrefix + deployed + ";\n";
502+
std::string topLevel =
503+
(topName + "(.text);\n" + dataOffsetPrefix + deployed + " = .;\n" +
504+
deployed + "(.text);\n" + dataSizePrefix + deployed + " = . - " +
505+
dataOffsetPrefix + deployed + ";\n")
506+
.str();
505507

506508
// Contains symbols whose values are the sizes of the dependent contracts.
507509
// For the example above, this contains:
@@ -541,9 +543,10 @@ static std::string creteEVMLinkerScript(ArrayRef<LLVMMemoryBufferRef> memBufs,
541543
// Emit size of the deploy code offset as the 4-byte unsigned integer.
542544
// This is needed to determine which offset the deployed code starts at
543545
// in the linked binary.
544-
Twine deploySize = "LONG(" + dataOffsetPrefix + deployed + ");\n";
546+
std::string deploySize =
547+
("LONG(" + dataOffsetPrefix + deployed + ");\n").str();
545548

546-
Twine script = formatv("{0}\n\
549+
std::string script = formatv("{0}\n\
547550
ENTRY(0);\n\
548551
SECTIONS {\n\
549552
. = 0;\n\
@@ -555,10 +558,10 @@ SECTIONS {\n\
555558
}\n\
556559
}\n\
557560
",
558-
symDatasizeDeps, topLevel, symDataOffsetDeps,
559-
symDatasizeTop, deploySize);
561+
symDatasizeDeps, topLevel, symDataOffsetDeps,
562+
symDatasizeTop, deploySize);
560563

561-
return script.str();
564+
return script;
562565
}
563566

564567
LLVMBool LLVMLinkEVM(LLVMMemoryBufferRef inBuffers[],
@@ -588,11 +591,11 @@ LLVMBool LLVMLinkEVM(LLVMMemoryBufferRef inBuffers[],
588591

589592
// Use remapping of file names (a linker feature) to replace file names with
590593
// indexes in the array of memory buffers.
591-
Twine remapStr("--remap-inputs=");
592-
std::string remapDeployStr = (remapStr + inBuffersIDs[0] + "=0").str();
594+
const std::string remapStr("--remap-inputs=");
595+
std::string remapDeployStr = remapStr + inBuffersIDs[0] + "=0";
593596
lldArgs.push_back(remapDeployStr.c_str());
594597

595-
std::string remapDeployedStr = (remapStr + inBuffersIDs[1] + "=1").str();
598+
std::string remapDeployedStr = remapStr + inBuffersIDs[1] + "=1";
596599
lldArgs.push_back(remapDeployedStr.c_str());
597600

598601
lldArgs.push_back("--remap-inputs=script.x=2");

llvm/lib/Target/EVM/EVMISelLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ SDValue EVMTargetLowering::lowerIntrinsicDataSize(unsigned IntrID, SDValue Op,
167167
const MDNode *Metadata = cast<MDNodeSDNode>(Op.getOperand(1))->getMD();
168168
StringRef ContractID = cast<MDString>(Metadata->getOperand(0))->getString();
169169
bool IsDataSize = IntrID == Intrinsic::evm_datasize;
170-
Twine SymbolReloc =
171-
Twine(IsDataSize ? "__datasize_" : "__dataoffset_") + ContractID;
170+
std::string SymbolReloc =
171+
(Twine(IsDataSize ? "__datasize_" : "__dataoffset_") + ContractID).str();
172172
MCSymbol *Sym = MF.getContext().getOrCreateSymbol(SymbolReloc);
173173
return SDValue(
174174
DAG.getMachineNode(EVM::DATA, DL, Ty, DAG.getMCSymbol(Sym, MVT::i256)),

0 commit comments

Comments
 (0)