Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle integer overflow in for loops #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ matrix:
# - INSTALL_LUA="lua=5.2"
# - LUA="lua"
- os: linux
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-5
env:
- CC="gcc-5"
- INSTALL_LUA="lua=5.3"
- LUA="lua"
# - os: linux
Expand Down Expand Up @@ -49,6 +56,7 @@ matrix:
# - LUA="luajit"

before_install:
- export CC=$CC
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
- pip install hererocks
- hererocks lua_install -rlatest --$INSTALL_LUA
Expand Down
38 changes: 33 additions & 5 deletions spec/coder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local function generate(ast, modname)
local ok, errmsg = util.set_file_contents(modname .. ".c", generated_code)
if not ok then return ok, errmsg end

local CC = "gcc"
local CC = os.getenv("CC") or "gcc"
local CFLAGS = "--std=c99 -O2 -Wall -Ilua/src/ -fPIC"

local cc_cmd = string.format([[
Expand All @@ -26,6 +26,16 @@ local function call(modname, code)
return os.execute(cmd)
end

local function compile(titan_code, lua_testcase)
local ast = parser.parse(titan_code)
local ok, err = checker.check(ast, titan_code, "test.titan")
assert.truthy(ok, err)
ok, err = generate(ast, "titan_test")
assert.truthy(ok, err)
ok, err = call("titan_test", lua_testcase)
return ok, err, ast
end

describe("Titan code generator", function()
it("deletes array element", function()
local code = [[
Expand Down Expand Up @@ -122,7 +132,7 @@ describe("Titan code generator", function()
assert.truthy(ok, err)
end)

it("tests integer postive literals in 'for'", function()
it("tests integer positive literals in 'for'", function()
local code = [[
function forstep(): integer
local v: integer = 0
Expand Down Expand Up @@ -222,6 +232,24 @@ describe("Titan code generator", function()
assert.truthy(ok, err)
end)

it("avoids integer overflow in 'for' edge case", function()
assert.truthy(compile([[
function bignumbers(nth: integer):integer
local c = 0
for i = 0x7FFFFFFFFFFFFFF8,0x7FFFFFFFFFFFFFFF do
c = c + 1
if c == nth then
return i
end
end
return 0
end
]], [[
assert(titan_test.bignumbers(8) == 9223372036854775807)
assert(titan_test.bignumbers(9) == 0)
]]))
end)

it("tests nil element in 'not'", function()
local code = [[
function testset(t: {integer}, i: integer, v: integer): integer
Expand Down Expand Up @@ -491,7 +519,7 @@ describe("Titan code generator", function()
it("generates code for array module-local variables", function()
local code = [[
local a: {integer} = {}
function len(): integer
function len(): integer
return #a
end
function seta(x: {integer}): nil
Expand Down Expand Up @@ -597,7 +625,7 @@ describe("Titan code generator", function()
it("generates code for exported array variables", function()
local code = [[
a: {integer} = {}
function len(): integer
function len(): integer
return #a
end
]]
Expand All @@ -613,7 +641,7 @@ describe("Titan code generator", function()

it("generates code for string length", function()
local code = [[
function len(a: string): integer
function len(a: string): integer
return #a
end
]]
Expand Down
24 changes: 16 additions & 8 deletions titan-compiler/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,11 @@ local function codefor(ctx, node)
local csstats, csexp = codeexp(ctx, node.start)
local cfstats, cfexp = codeexp(ctx, node.finish)
local cinc = ""
local covf = ""
local cvtyp
if types.equals(node.decl._type, types.Integer) then
cvtyp = "lua_Integer"
covf = "int _foroverflow = 0;"
else
cvtyp = "lua_Number"
end
Expand Down Expand Up @@ -382,22 +384,24 @@ local function codefor(ctx, node)
local tmpl
if types.equals(node.decl._type, types.Integer) then
subs.ILIT = c_integer_literal(ilit)
tmpl = "$CVAR = l_castU2S(l_castS2U($CVAR) + $ILIT)"
tmpl = "_foroverflow = __builtin_add_overflow($CVAR, $ILIT, &($CVAR))"
ccmp = render("(!_foroverflow) && ($CVAR <= _forlimit)", subs)
else
subs.ILIT = c_float_literal(ilit)
tmpl = "$CVAR += $ILIT"
ccmp = render("$CVAR <= _forlimit", subs)
end
cstep = render(tmpl, subs)
ccmp = render("$CVAR <= _forlimit", subs)
else
if types.equals(node.decl._type, types.Integer) then
subs.NEGILIT = c_integer_literal(-ilit)
cstep = render("$CVAR = l_castU2S(l_castS2U($CVAR) - $NEGILIT)", subs)
cstep = render("_foroverflow = __builtin_sub_overflow($CVAR, $NEGILIT, &($CVAR))", subs)
ccmp = render("(!_foroverflow) && (_forlimit <= $CVAR)", subs)
else
subs.NEGILIT = c_float_literal(-ilit)
cstep = render("$CVAR -= $NEGILIT", subs)
ccmp = render("_forlimit <= $CVAR", subs)
end
ccmp = render("_forlimit <= $CVAR", subs)
end
else
local cistats, ciexp = codeexp(ctx, node.inc)
Expand All @@ -411,20 +415,22 @@ local function codefor(ctx, node)
})
local tmpl
if types.equals(node.decl._type, types.Integer) then
tmpl = "$CVAR = l_castU2S(l_castS2U($CVAR) + l_castS2U(_forstep))"
tmpl = "_foroverflow = __builtin_add_overflow($CVAR, _forstep, &($CVAR))"
ccmp = render("(!_foroverflow) && (0 < _forstep ? ($CVAR <= _forlimit) : (_forlimit <= $CVAR))", subs)
else
tmpl = "$CVAR += _forstep"
ccmp = render("0 < _forstep ? ($CVAR <= _forlimit) : (_forlimit <= $CVAR)", subs)
end
cstep = render(tmpl, subs)
ccmp = render("0 < _forstep ? ($CVAR <= _forlimit) : (_forlimit <= $CVAR)", subs)
end
else
if types.equals(node.decl._type, types.Integer) then
cstep = render("$CVAR = l_castU2S(l_castS2U($CVAR) + 1)", subs)
cstep = render("_foroverflow = __builtin_add_overflow($CVAR, 1, &($CVAR))", subs)
ccmp = render("(!_foroverflow) && ($CVAR <= _forlimit)", subs)
else
cstep = render("$CVAR += 1.0", subs)
ccmp = render("$CVAR <= _forlimit", subs)
end
ccmp = render("$CVAR <= _forlimit", subs)
end
local nallocs = ctx.allocations
local cblock = codestat(ctx, node.block)
Expand All @@ -435,6 +441,7 @@ local function codefor(ctx, node)
$CSTART
$CFINISH
$CINC
$COVF
for($CDECL = _forstart; $CCMP; $CSTEP) {
$CBLOCK
$CHECKGC
Expand All @@ -448,6 +455,7 @@ local function codefor(ctx, node)
CCMP = ccmp,
CSTEP = cstep,
CBLOCK = cblock,
COVF = covf,
CHECKGC = nallocs > 0 and "luaC_checkGC(L);" or ""
})
end
Expand Down