[WebAssembly] Relax signature checking for undefined functions that are not called directly

When function signatures don't match and the undefined function is not
called directly (i.e. only has its address taken) we don't issue a
warning or create a runtime thunk for the undefined function.

Instead in this case we simply use the defined version of the function.
This is possible since checking signatures of dynamic calls happens
at runtime so any invalid usage will still result in a runtime error.

This is needed to allow C++ programs to link without generating
warnings.  Its not uncommon in C++ for vtables to be populated by
function address whee the signature of the function is not known in the
compilation unit.  In this case clang declares the method as void(void)
and relies on the vtable caller casting the data back to the correct
signature.

Fixes: https://bugs.llvm.org/show_bug.cgi?id=40412

Differential Revision: https://reviews.llvm.org/D62153

llvm-svn: 361678
This commit is contained in:
Sam Clegg 2019-05-24 22:45:08 +00:00
parent bede937b16
commit 59f959ff33
7 changed files with 65 additions and 21 deletions

View File

@ -0,0 +1,19 @@
; RUN: llc -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o
; RUN: llc -filetype=obj %s -o %t.main.o
; RUN: wasm-ld --fatal-warnings -o %t.wasm %t.ret32.o %t.main.o
; RUN: wasm-ld --fatal-warnings -o %t.wasm %t.main.o %t.ret32.o
target triple = "wasm32-unknown-unknown"
; Function declartion with incorrect signature.
declare dso_local void @ret32()
; Simply taking the address of the function should *not* generate the
; the signature mismatch warning.
@ptr = dso_local global i8* bitcast (void ()* @ret32 to i8*), align 8
define hidden void @_start() local_unnamed_addr {
%addr = load i32 ()*, i32 ()** bitcast (i8** @ptr to i32 ()**), align 8
call i32 %addr()
ret void
}

View File

@ -550,7 +550,7 @@ struct WrappedSymbol {
}; };
static Symbol *addUndefined(StringRef Name) { static Symbol *addUndefined(StringRef Name) {
return Symtab->addUndefinedFunction(Name, "", "", 0, nullptr, nullptr); return Symtab->addUndefinedFunction(Name, "", "", 0, nullptr, nullptr, false);
} }
// Handles -wrap option. // Handles -wrap option.

View File

@ -271,14 +271,16 @@ void ObjFile::parse(bool IgnoreComdats) {
} }
} }
// Find the code and data sections. Wasm objects can have at most one code
// and one data section.
uint32_t SectionIndex = 0; uint32_t SectionIndex = 0;
SymbolIsCalledDirectly.resize(WasmObj->getNumberOfSymbols(), false);
for (const SectionRef &Sec : WasmObj->sections()) { for (const SectionRef &Sec : WasmObj->sections()) {
const WasmSection &Section = WasmObj->getWasmSection(Sec); const WasmSection &Section = WasmObj->getWasmSection(Sec);
// Wasm objects can have at most one code and one data section.
if (Section.Type == WASM_SEC_CODE) { if (Section.Type == WASM_SEC_CODE) {
assert(!CodeSection);
CodeSection = &Section; CodeSection = &Section;
} else if (Section.Type == WASM_SEC_DATA) { } else if (Section.Type == WASM_SEC_DATA) {
assert(!DataSection);
DataSection = &Section; DataSection = &Section;
} else if (Section.Type == WASM_SEC_CUSTOM) { } else if (Section.Type == WASM_SEC_CUSTOM) {
CustomSections.emplace_back(make<InputSection>(Section, this)); CustomSections.emplace_back(make<InputSection>(Section, this));
@ -286,6 +288,11 @@ void ObjFile::parse(bool IgnoreComdats) {
CustomSectionsByIndex[SectionIndex] = CustomSections.back(); CustomSectionsByIndex[SectionIndex] = CustomSections.back();
} }
SectionIndex++; SectionIndex++;
// Scans relocations to dermine determine if a function symbol is called
// directly
for (const WasmRelocation &Reloc : Section.Relocations)
if (Reloc.Type == R_WASM_FUNCTION_INDEX_LEB)
SymbolIsCalledDirectly[Reloc.Index] = true;
} }
TypeMap.resize(getWasmObj()->types().size()); TypeMap.resize(getWasmObj()->types().size());
@ -326,10 +333,16 @@ void ObjFile::parse(bool IgnoreComdats) {
Symbols.reserve(WasmObj->getNumberOfSymbols()); Symbols.reserve(WasmObj->getNumberOfSymbols());
for (const SymbolRef &Sym : WasmObj->symbols()) { for (const SymbolRef &Sym : WasmObj->symbols()) {
const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl()); const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
if (Symbol *Sym = createDefined(WasmSym)) if (WasmSym.isDefined()) {
Symbols.push_back(Sym); // createDefined may fail if the symbol is comdat excluded in which case
else // we fall back to creating an undefined symbol
Symbols.push_back(createUndefined(WasmSym)); if (Symbol *D = createDefined(WasmSym)) {
Symbols.push_back(D);
continue;
}
}
size_t Idx = Symbols.size();
Symbols.push_back(createUndefined(WasmSym, SymbolIsCalledDirectly[Idx]));
} }
} }
@ -361,9 +374,6 @@ DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
} }
Symbol *ObjFile::createDefined(const WasmSymbol &Sym) { Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
if (!Sym.isDefined())
return nullptr;
StringRef Name = Sym.Info.Name; StringRef Name = Sym.Info.Name;
uint32_t Flags = Sym.Info.Flags; uint32_t Flags = Sym.Info.Flags;
@ -417,7 +427,7 @@ Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
llvm_unreachable("unknown symbol kind"); llvm_unreachable("unknown symbol kind");
} }
Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) { Symbol *ObjFile::createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly) {
StringRef Name = Sym.Info.Name; StringRef Name = Sym.Info.Name;
uint32_t Flags = Sym.Info.Flags; uint32_t Flags = Sym.Info.Flags;
@ -425,7 +435,7 @@ Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) {
case WASM_SYMBOL_TYPE_FUNCTION: case WASM_SYMBOL_TYPE_FUNCTION:
return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName, return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName,
Sym.Info.ImportModule, Flags, this, Sym.Info.ImportModule, Flags, this,
Sym.Signature); Sym.Signature, IsCalledDirectly);
case WASM_SYMBOL_TYPE_DATA: case WASM_SYMBOL_TYPE_DATA:
return Symtab->addUndefinedData(Name, Flags, this); return Symtab->addUndefinedData(Name, Flags, this);
case WASM_SYMBOL_TYPE_GLOBAL: case WASM_SYMBOL_TYPE_GLOBAL:
@ -499,7 +509,7 @@ static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
if (ObjSym.isUndefined() || ExcludedByComdat) { if (ObjSym.isUndefined() || ExcludedByComdat) {
if (ObjSym.isExecutable()) if (ObjSym.isExecutable())
return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F, return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F,
nullptr); nullptr, true);
return Symtab->addUndefinedData(Name, Flags, &F); return Symtab->addUndefinedData(Name, Flags, &F);
} }

View File

@ -69,6 +69,13 @@ protected:
// List of all symbols referenced or defined by this file. // List of all symbols referenced or defined by this file.
std::vector<Symbol *> Symbols; std::vector<Symbol *> Symbols;
// Bool for each symbol, true if called directly. This allows us to implement
// a weaker form of signature checking where undefined functions that are not
// called directly (i.e. only address taken) don't have to match the defined
// function's signature. We cannot do this for directly called functions
// because those signatures are checked at validation times.
// See https://bugs.llvm.org/show_bug.cgi?id=40412
std::vector<bool> SymbolIsCalledDirectly;
private: private:
const Kind FileKind; const Kind FileKind;
@ -138,7 +145,7 @@ public:
private: private:
Symbol *createDefined(const WasmSymbol &Sym); Symbol *createDefined(const WasmSymbol &Sym);
Symbol *createUndefined(const WasmSymbol &Sym); Symbol *createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly);
bool isExcludedByComdat(InputChunk *Chunk) const; bool isExcludedByComdat(InputChunk *Chunk) const;

View File

@ -286,7 +286,11 @@ Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
return S; return S;
} }
if (Function && !signatureMatches(ExistingFunction, &Function->Signature)) { bool CheckSig = true;
if (auto UD = dyn_cast<UndefinedFunction>(ExistingFunction))
CheckSig = UD->IsCalledDirectly;
if (CheckSig && Function && !signatureMatches(ExistingFunction, &Function->Signature)) {
Symbol* Variant; Symbol* Variant;
if (getFunctionVariant(S, &Function->Signature, File, &Variant)) if (getFunctionVariant(S, &Function->Signature, File, &Variant))
// New variant, always replace // New variant, always replace
@ -384,7 +388,8 @@ Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags,
Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName, Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
StringRef ImportModule, StringRef ImportModule,
uint32_t Flags, InputFile *File, uint32_t Flags, InputFile *File,
const WasmSignature *Sig) { const WasmSignature *Sig,
bool IsCalledDirectly) {
LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name <<
" [" << (Sig ? toString(*Sig) : "none") << "]\n"); " [" << (Sig ? toString(*Sig) : "none") << "]\n");
@ -396,7 +401,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
auto Replace = [&]() { auto Replace = [&]() {
replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags, replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags,
File, Sig); File, Sig, IsCalledDirectly);
}; };
if (WasInserted) if (WasInserted)
@ -409,7 +414,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION); reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
return S; return S;
} }
if (!signatureMatches(ExistingFunction, Sig)) if (IsCalledDirectly && !signatureMatches(ExistingFunction, Sig))
if (getFunctionVariant(S, Sig, File, &S)) if (getFunctionVariant(S, Sig, File, &S))
Replace(); Replace();
} }

View File

@ -63,7 +63,8 @@ public:
Symbol *addUndefinedFunction(StringRef Name, StringRef ImportName, Symbol *addUndefinedFunction(StringRef Name, StringRef ImportName,
StringRef ImportModule, uint32_t Flags, StringRef ImportModule, uint32_t Flags,
InputFile *File, const WasmSignature *Signature); InputFile *File, const WasmSignature *Signature,
bool IsCalledDirectly);
Symbol *addUndefinedData(StringRef Name, uint32_t Flags, InputFile *File); Symbol *addUndefinedData(StringRef Name, uint32_t Flags, InputFile *File);
Symbol *addUndefinedGlobal(StringRef Name, StringRef ImportName, Symbol *addUndefinedGlobal(StringRef Name, StringRef ImportName,
StringRef ImportModule, uint32_t Flags, StringRef ImportModule, uint32_t Flags,

View File

@ -194,9 +194,10 @@ public:
UndefinedFunction(StringRef Name, StringRef ImportName, UndefinedFunction(StringRef Name, StringRef ImportName,
StringRef ImportModule, uint32_t Flags, StringRef ImportModule, uint32_t Flags,
InputFile *File = nullptr, InputFile *File = nullptr,
const WasmSignature *Type = nullptr) const WasmSignature *Type = nullptr,
bool IsCalledDirectly = true)
: FunctionSymbol(Name, UndefinedFunctionKind, Flags, File, Type), : FunctionSymbol(Name, UndefinedFunctionKind, Flags, File, Type),
ImportName(ImportName), ImportModule(ImportModule) {} ImportName(ImportName), ImportModule(ImportModule), IsCalledDirectly(IsCalledDirectly) {}
static bool classof(const Symbol *S) { static bool classof(const Symbol *S) {
return S->kind() == UndefinedFunctionKind; return S->kind() == UndefinedFunctionKind;
@ -204,6 +205,7 @@ public:
StringRef ImportName; StringRef ImportName;
StringRef ImportModule; StringRef ImportModule;
bool IsCalledDirectly;
}; };
// Section symbols for output sections are different from those for input // Section symbols for output sections are different from those for input