Added better sanitizer and sanitized the source code.

This commit is contained in:
kobalicekp
2014-02-02 21:39:09 +01:00
parent 0e84072227
commit 2aba235875
12 changed files with 167 additions and 50 deletions

View File

@@ -5,8 +5,8 @@
// Zlib - See LICENSE.md file in the package. // Zlib - See LICENSE.md file in the package.
// [Guard] // [Guard]
#ifndef _GENBLEND_H #ifndef _APP_TEST_GENBLEND_H
#define _GENBLEND_H #define _APP_TEST_GENBLEND_H
// [Dependencies] // [Dependencies]
#include <asmjit/asmjit.h> #include <asmjit/asmjit.h>
@@ -175,4 +175,4 @@ static void blend(asmjit::host::Compiler& c) {
} // asmgen namespace } // asmgen namespace
// [Guard] // [Guard]
#endif // _GENBLEND_H #endif // _APP_TEST_GENBLEND_H

View File

@@ -5,8 +5,8 @@
// Zlib - See LICENSE.md file in the package. // Zlib - See LICENSE.md file in the package.
// [Guard] // [Guard]
#ifndef _GENOPCODE_H #ifndef _APP_TEST_GENOPCODE_H
#define _GENOPCODE_H #define _APP_TEST_GENOPCODE_H
// [Dependencies] // [Dependencies]
#include <asmjit/asmjit.h> #include <asmjit/asmjit.h>
@@ -2307,4 +2307,4 @@ static void opcode(asmjit::host::Assembler& a) {
} // asmgen namespace } // asmgen namespace
// [Guard] // [Guard]
#endif // _GENOPCODE_H #endif // _APP_TEST_GENOPCODE_H

View File

@@ -975,7 +975,7 @@ struct X86Test_AllocStack : public X86Test {
// Fill stack by sequence [0, 1, 2, 3 ... 255]. // Fill stack by sequence [0, 1, 2, 3 ... 255].
c.xor_(i, i); c.xor_(i, i);
c.bind(L_1); c.bind(L_1);
c.mov(stack.clone().setIndex(i, 0), i.r8()); c.mov(stack.clone().setIndex(i, 0), i.r8());
c.inc(i); c.inc(i);
@@ -985,7 +985,7 @@ struct X86Test_AllocStack : public X86Test {
// Sum sequence in stack. // Sum sequence in stack.
c.xor_(i, i); c.xor_(i, i);
c.xor_(a, a); c.xor_(a, a);
c.bind(L_2); c.bind(L_2);
c.movzx(b, stack.clone().setIndex(i, 0)); c.movzx(b, stack.clone().setIndex(i, 0));
c.add(a, b); c.add(a, b);
@@ -1709,7 +1709,7 @@ X86TestSuite::X86TestSuite() :
ADD_TEST(X86Test_AllocStack); ADD_TEST(X86Test_AllocStack);
ADD_TEST(X86Test_AllocMemcpy); ADD_TEST(X86Test_AllocMemcpy);
ADD_TEST(X86Test_AllocBlend); ADD_TEST(X86Test_AllocBlend);
// Call. // Call.
ADD_TEST(X86Test_CallBase); ADD_TEST(X86Test_CallBase);
ADD_TEST(X86Test_CallFast); ADD_TEST(X86Test_CallFast);

View File

@@ -186,7 +186,7 @@ Error BaseContext::resolveCellOffsets() {
uint32_t stackAlignment = 0; uint32_t stackAlignment = 0;
if (stackCell != NULL) if (stackCell != NULL)
stackAlignment = stackCell->getAlignment(); stackAlignment = stackCell->getAlignment();
uint32_t pos64 = 0; uint32_t pos64 = 0;
uint32_t pos32 = pos64 + _mem64ByteVarsUsed * 64; uint32_t pos32 = pos64 + _mem64ByteVarsUsed * 64;
@@ -200,7 +200,7 @@ Error BaseContext::resolveCellOffsets() {
uint32_t gapAlignment = stackAlignment; uint32_t gapAlignment = stackAlignment;
uint32_t gapSize = 0; uint32_t gapSize = 0;
if (gapAlignment) if (gapAlignment)
IntUtil::deltaTo(stackPos, gapAlignment); IntUtil::deltaTo(stackPos, gapAlignment);
stackPos += gapSize; stackPos += gapSize;
@@ -301,7 +301,7 @@ Error BaseContext::removeUnreachableCode() {
void BaseContext::cleanup() { void BaseContext::cleanup() {
VarData** array = _contextVd.getData(); VarData** array = _contextVd.getData();
size_t length = _contextVd.getLength(); size_t length = _contextVd.getLength();
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
VarData* vd = array[i]; VarData* vd = array[i];
vd->resetContextId(); vd->resetContextId();

View File

@@ -5,8 +5,8 @@
// Zlib - See LICENSE.md file in the package. // Zlib - See LICENSE.md file in the package.
// [Guard] // [Guard]
#ifndef _ASMJIT_BASE_CONTEXT_H #ifndef _ASMJIT_BASE_CONTEXT_P_H
#define _ASMJIT_BASE_CONTEXT_H #define _ASMJIT_BASE_CONTEXT_P_H
// [Dependencies - AsmJit] // [Dependencies - AsmJit]
#include "../base/compiler.h" #include "../base/compiler.h"
@@ -251,4 +251,4 @@ struct BaseContext {
#include "../base/apiend.h" #include "../base/apiend.h"
// [Guard] // [Guard]
#endif // _ASMJIT_BASE_CONTEXT_H #endif // _ASMJIT_BASE_CONTEXT_P_H

View File

@@ -56,7 +56,7 @@ struct WinRemoteRuntime : public BaseRuntime {
//! @brief Process. //! @brief Process.
HANDLE _hProcess; HANDLE _hProcess;
//! @brief Virtual memory manager. //! @brief Virtual memory manager.
VirtualMemoryManager _memoryManager; VirtualMemoryManager _memoryManager;
}; };

View File

@@ -250,7 +250,7 @@ namespace x86x64 {
//! to create invalid instruction (for example <code>mov [eax], [eax]</code>, //! to create invalid instruction (for example <code>mov [eax], [eax]</code>,
//! because such overload doesn't exist. //! because such overload doesn't exist.
//! //!
//! Each call to an assembler intrinsic function emits instruction directly //! Each call to an assembler intrinsic function emits instruction directly
//! to the binary stream. There are also runtime checks that prevent invalid //! to the binary stream. There are also runtime checks that prevent invalid
//! code to be emitted. It will assert in debug mode and put the @ref Assembler //! code to be emitted. It will assert in debug mode and put the @ref Assembler
//! instance to an error state in production mode. //! instance to an error state in production mode.

View File

@@ -443,7 +443,7 @@ Error X86X64Compiler::setArg(uint32_t argIndex, BaseVar& var) {
Error X86X64Compiler::_newVar(BaseVar* var, uint32_t vType, const char* name) { Error X86X64Compiler::_newVar(BaseVar* var, uint32_t vType, const char* name) {
ASMJIT_ASSERT(vType < kVarTypeCount); ASMJIT_ASSERT(vType < kVarTypeCount);
vType = _targetVarMapping[vType]; vType = _targetVarMapping[vType];
const VarInfo& vInfo = _varInfo[vType]; const VarInfo& vInfo = _varInfo[vType];

View File

@@ -1910,7 +1910,7 @@ _NextGroup:
uint32_t inReg = special[i].inReg; uint32_t inReg = special[i].inReg;
uint32_t outReg = special[i].outReg; uint32_t outReg = special[i].outReg;
uint32_t c; uint32_t c;
if (static_cast<const X86Reg*>(op)->isGp()) if (static_cast<const X86Reg*>(op)->isGp())
c = kRegClassGp; c = kRegClassGp;
else else

View File

@@ -11,10 +11,10 @@
// [Dependencies - AsmJit] // [Dependencies - AsmJit]
#include "../base/assembler.h" #include "../base/assembler.h"
#include "../base/assert.h" #include "../base/assert.h"
#include "../base/defs.h"
#include "../base/vectypes.h"
#include "../base/compiler.h" #include "../base/compiler.h"
#include "../base/defs.h"
#include "../base/intutil.h" #include "../base/intutil.h"
#include "../base/vectypes.h"
// [Api-Begin] // [Api-Begin]
#include "../base/apibegin.h" #include "../base/apibegin.h"
@@ -2543,7 +2543,7 @@ struct Mem : public BaseMem {
_init_packed_op_sz_b0_b1_id(kOperandTypeMem, size, kMemTypeBaseIndex, _init_packed_op_sz_b0_b1_id(kOperandTypeMem, size, kMemTypeBaseIndex,
_getGpdFlags(reinterpret_cast<const BaseVar&>(base)) _getGpdFlags(reinterpret_cast<const BaseVar&>(base))
+ (kMemVSibXmm << kMemVSibIndex) + (kMemVSibXmm << kMemVSibIndex)
+ (shift << kMemShiftIndex), + (shift << kMemShiftIndex),
_OP_ID(base)); _OP_ID(base));
_vmem.index = _OP_ID(index); _vmem.index = _OP_ID(index);
@@ -2555,7 +2555,7 @@ struct Mem : public BaseMem {
_init_packed_op_sz_b0_b1_id(kOperandTypeMem, size, kMemTypeBaseIndex, _init_packed_op_sz_b0_b1_id(kOperandTypeMem, size, kMemTypeBaseIndex,
_getGpdFlags(reinterpret_cast<const BaseVar&>(base)) _getGpdFlags(reinterpret_cast<const BaseVar&>(base))
+ (kMemVSibYmm << kMemVSibIndex) + (kMemVSibYmm << kMemVSibIndex)
+ (shift << kMemShiftIndex), + (shift << kMemShiftIndex),
_OP_ID(base)); _OP_ID(base));
_vmem.index = _OP_ID(index); _vmem.index = _OP_ID(index);

View File

@@ -195,7 +195,7 @@ static uint32_t X86X64FuncDecl_initConv(X86X64FuncDecl* self, uint32_t arch, uin
} }
#undef R #undef R
static Error X86X64FuncDecl_initFunc(X86X64FuncDecl* self, uint32_t arch, static Error X86X64FuncDecl_initFunc(X86X64FuncDecl* self, uint32_t arch,
uint32_t ret, const uint32_t* argList, uint32_t argCount) { uint32_t ret, const uint32_t* argList, uint32_t argCount) {
ASMJIT_ASSERT(argCount <= kFuncArgCount); ASMJIT_ASSERT(argCount <= kFuncArgCount);

View File

@@ -15,17 +15,19 @@ var filesToSanitize = (function() {
var fullName = path.normalize(path.join(dir, baseName)); var fullName = path.normalize(path.join(dir, baseName));
var stat = fs.lstatSync(fullName); var stat = fs.lstatSync(fullName);
if (stat.isSymbolicLink()) if (stat.isSymbolicLink()) {
continue; continue;
}
if (stat.isDirectory()) { if (stat.isDirectory()) {
listPrivate(subarray, path.join(dir, baseName), displayDir + baseName, accept); subarray = listPrivate(subarray,
path.join(dir, baseName), displayDir ? displayDir + "/" + baseName : baseName, accept);
continue; continue;
} }
if (stat.isFile()) { if (stat.isFile()) {
if (accept(baseName)) if (accept(baseName))
array.push({ name: fullName, display: displayDir + baseName }); array.push({ name: fullName, display: displayDir ? displayDir + "/" + baseName : baseName });
continue; continue;
} }
} }
@@ -38,6 +40,19 @@ var filesToSanitize = (function() {
}; };
})(); })();
/**
* Inject data into string.
*/
var inject = function(s, start, end, what) {
assert(start <= s.length);
assert(end <= s.length);
return s.substr(0, start) + what + s.substr(end);
};
/**
* Is the extension c++ header file?
*/
var isCppHeaderExt = function(ext) { var isCppHeaderExt = function(ext) {
return ext === ".h" || return ext === ".h" ||
ext === ".hh" || ext === ".hh" ||
@@ -45,6 +60,9 @@ var isCppHeaderExt = function(ext) {
ext === ".hxx" ; ext === ".hxx" ;
}; };
/**
* Is the extension c++ source file?
*/
var isCppSourceExt = function(ext) { var isCppSourceExt = function(ext) {
return ext === ".c" || return ext === ".c" ||
ext === ".cc" || ext === ".cc" ||
@@ -66,7 +84,10 @@ var filesToAccept = function(name) {
ext === ".mm" ; ext === ".mm" ;
}; };
var sanitySpaces = function(data) { /**
* Sanity spaces.
*/
var sanitySpaces = function(data, name) {
// Remove carriage return. // Remove carriage return.
data = data.replace(/\r\n/g, "\n"); data = data.replace(/\r\n/g, "\n");
// Remove spaces before the end of the line. // Remove spaces before the end of the line.
@@ -77,12 +98,109 @@ var sanitySpaces = function(data) {
return data; return data;
}; };
var sanityHeaderGuards = function(data) { /**
* Sanity header guards.
*/
var sanityHeaderGuards = (function() {
var parseGuardName = function(data, i) {
var m = data.substr(i).match(/[\w][\d\w]*/);
return m ? m[0] : null;
};
return data; var makeGuardName = function(name) {
}; // Remove leading '/' or '\'.
if (/^[\\\/]/.test(name))
name = name.substr(1);
return "_" + name.toUpperCase().replace(/[\/\\\.-]/g, "_");
};
var sanityIncludeOrder = function(data, directive) { var directiveMarks = [
"#ifndef ",
"#endif // ",
"#define "
];
var directiveNames = [
"#ifndef ",
"#endif ",
"#define "
];
return function(data, name) {
var i = 0;
var nl = true;
var guard = "// " + "[Guard]" + "\n";
var nFound = 0;
while (i < data.length) {
if (nl && data.substr(i, guard.length) === guard) {
i += guard.length;
nFound++;
if (i >= data.length)
break;
for (var j = 0; j < directiveMarks.length; ) {
var m = directiveMarks[j];
if (data.substr(i, m.length) === m && data.charAt(i + m.length) === '_') {
i += directiveMarks[j].length;
var oldGuardName = parseGuardName(data, i);
var newGuardName;
if (oldGuardName) {
console.log(oldGuardName);
var startPosition = i;
var endPosition = i + oldGuardName.length;
newGuardName = makeGuardName(name);
if (oldGuardName !== newGuardName) {
console.log(name + ": " + directiveNames[j] + newGuardName);
data = inject(data, startPosition, endPosition, newGuardName);
i += newGuardName.length;
i = data.indexOf('\n', i);
if (i === -1) {
// Terminates the loop.
i = data.length;
j = 9999;
nl = false;
break;
}
else {
i++;
}
}
}
j += 2;
}
// Don't process '#define' directive if previous '#ifndef' wasn't matched.
else {
if (++j == 2)
break;
}
}
}
else {
nl = data.charAt(i) === '\n';
i++;
}
}
if (nFound & 1) {
console.log(name + ": Odd number of guards found: " + nFound);
}
return data;
};
})();
/**
* Sanity #include order.
*/
var sanityIncludeOrder = function(data, name, directive) {
var i = 0; var i = 0;
var nl = true; var nl = true;
@@ -92,7 +210,7 @@ var sanityIncludeOrder = function(data, directive) {
var replacement; var replacement;
while (i < data.length) { while (i < data.length) {
if (nl && data.indexOf(directive, i) === i) { if (nl && data.substr(i, directive.length) === directive) {
var iLocal = i var iLocal = i
if (startPosition === -1) { if (startPosition === -1) {
@@ -105,7 +223,7 @@ var sanityIncludeOrder = function(data, directive) {
list.push(data.substring(iLocal, i)); list.push(data.substring(iLocal, i));
break; break;
} }
if (data[i] === '\n') { if (data.charAt(i) === '\n') {
list.push(data.substring(iLocal, i)); list.push(data.substring(iLocal, i));
i++; i++;
break; break;
@@ -118,13 +236,10 @@ var sanityIncludeOrder = function(data, directive) {
if (list.length > 1) { if (list.length > 1) {
list.sort(); list.sort();
replacement = list.join("\n"); replacement = list.join("\n") + "\n";
assert(replacement.length == endPosition - startPosition - 1);
data = data.substring(0, startPosition) + assert(replacement.length == endPosition - startPosition);
replacement + data = inject(data, startPosition, endPosition, replacement);
"\n" +
data.substring(endPosition);
} }
startPosition = -1; startPosition = -1;
@@ -135,7 +250,7 @@ var sanityIncludeOrder = function(data, directive) {
i++; i++;
} }
else { else {
nl = data[i] === '\n'; nl = data.charAt(i) === '\n';
i++; i++;
} }
} }
@@ -143,32 +258,34 @@ var sanityIncludeOrder = function(data, directive) {
return data; return data;
}; };
/**
* Sanity the given data of file.
*/
var sanity = function(data, name) { var sanity = function(data, name) {
var ext = path.extname(name).toLowerCase(); var ext = path.extname(name).toLowerCase();
// Sanity spaces. // Sanity spaces.
data = sanitySpaces(data); data = sanitySpaces(data, name);
// Fix C/C++ header guards. // Fix C/C++ header guards and sort '#include' files.
if (isCppHeaderExt(ext)) { if (isCppHeaderExt(ext)) {
data = sanityHeaderGuards(data); data = sanityHeaderGuards(data, name);
} data = sanityIncludeOrder(data, name, "#include");
// Sort #include files.
if (isCppHeaderExt(ext) || isCppSourceExt(ext)) {
data = sanityIncludeOrder(data, "#include");
} }
return data; return data;
}; };
/**
* Entry.
*/
var main = function(dir) { var main = function(dir) {
filesToSanitize(dir, filesToAccept).forEach(function(file) { filesToSanitize(dir, filesToAccept).forEach(function(file) {
var oldData = fs.readFileSync(file.name, "utf8"); var oldData = fs.readFileSync(file.name, "utf8");
var newData = sanity(oldData, file.display); var newData = sanity(oldData, file.display);
if (oldData !== newData) { if (oldData !== newData) {
console.log("Sanitizing: " + file.display); console.log(file.display + ": Writing...");
fs.writeFileSync(file.name, newData, "utf8"); fs.writeFileSync(file.name, newData, "utf8");
} }
}); });