jconway / plr

PL/R - R Procedural Language for PostgreSQL
http://www.joeconway.com
90 stars 42 forks source link

wrong type of text crashes the server #22

Open rmesser opened 8 years ago

rmesser commented 8 years ago

I have a function that crashes my postgres 9.5 server. Here is the PL/R function:

CREATE OR REPLACE FUNCTION test_svg () RETURNS text AS
$$
library("svglite")
library("ggplot2")

# set up to output svg to a string
s = svgstring()

# create the plot, as an example
ggplot(mapping = aes(cars$speed)) + geom_density()

# NOTE: this works:
# plot(density(cars$speed))

# fetch the string
svg = s()

# turn off output to the string
dev.off()

return(svg)
$$
LANGUAGE 'plr' IMMUTABLE;

When I run this, the whole server crashes, and all connections are dropped. Here is the main part of the error message in my postgresql logs:

Error: CHAR() can only be applied to a 'CHARSXP', not a 'pairlist' * stack smashing detected *: postgres: postgres c7_70 [local] SELECT terminated ======= Backtrace: ========= /lib64/libc.so.6(__fortify_fail+0x37)[0x7fc78c616b37] /lib64/libc.so.6(__fortify_fail+0x0)[0x7fc78c616b00] /usr/lib64/R/lib/libR.so(+0x1412d1)[0x7fc73f2182d1]

Curiously, if I simply remove the return(svg) statement and instead do return("foo"), it works fine (but of course I don't get the output I want). So somehow the process of returning the svg string and coercing it to the postgresql text causes a hard crash. It is also odd to me that the regular old "plot" works just fine. Maybe that means this is a bug in svglite or ggplot2, but it would be nice if PL/R prevented server crashes as much as possible. Maybe it is something about the svg generated by ggplot2 that causes the problem.

As a side note, I am new to PL/R but find it confusing how returning text often fails. For example, if I have two strings and concatenate them, no dice. That is, this doesn't work:

CREATE OR REPLACE FUNCTION R_test_cat () RETURNS text AS $$ return(cat("foo","bar")) $$ language 'plr';

When running this I get nothing, whereas a simple return("foo bar") works just fine. If there is something I am doing wrong in either of these cases please advise. Thanks, and appreciate the efforts on PL/R, it is a very useful module.

kmatt commented 8 years ago

Might be good to submit this to pgsql-bugs@postgresql.org in case this is something that should be trapped in the engine, preventing a DB crash.

On Jun 16, 2016, at 7:04 PM, Robert Messer notifications@github.com wrote:

I have a function that crashes my postgres 9.5 server. Here is the PL/R function:

CREATE OR REPLACE FUNCTION test_svg () RETURNS text AS $$ library("svglite") library("ggplot2")

set up to output svg to a string

s = svgstring()

create the plot, as an example

ggplot(mapping = aes(cars$speed)) + geom_density()

NOTE: this works:

plot(density(cars$speed))

fetch the string

svg = s()

turn off output to the string

dev.off()

return(svg) $$ LANGUAGE 'plr' IMMUTABLE; When I run this, the whole server crashes, and all connection are dropped. Here is the main part of the error message in my postgresql logs:

Error: CHAR() can only be applied to a 'CHARSXP', not a 'pairlist' * stack smashing detected *: postgres: postgres c7_70 [local] SELECT terminated ======= Backtrace: ========= /lib64/libc.so.6(__fortify_fail+0x37)[0x7fc78c616b37] /lib64/libc.so.6(__fortify_fail+0x0)[0x7fc78c616b00] /usr/lib64/R/lib/libR.so(+0x1412d1)[0x7fc73f2182d1]

Curiously, if I simply remove the return statement and do return("foo"), it works fine (but of course I don't get the output I want. So somehow the process of returning the svg and coercing to the postgresql text causes a hard crash. It is also odd to me that the regular old "plot" works just fine. Maybe that means this is a bug in svglite or ggplot2, but it would be nice if PL/R prevented against server crashes as much as possible. Maybe it is something about the svg generated by ggplot2 that causes the problem.

As a side note, I am new to PL/R but find it confusing how returning text often fails. For example, if I have two string and concatenate them, no dice. That is, this doesn't work:

CREATE OR REPLACE FUNCTION R_test_cat () RETURNS text AS $$ return(cat("foo","bar")) $$ language 'plr'; When running this I get nothing, whereas a simple return("foo bar") works just fine. If there is something I am going wrong in either of these cases please advise. Thanks, and appreciate the efforts on PL/R, it is a very useful modules.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

davecramer commented 8 years ago

Unfortunately I don't think the backend can trap these things... Will look at this shortly. Thanks for the testcase

Dave Cramer

On 16 June 2016 at 21:32, MattK notifications@github.com wrote:

Might be good to submit this to pgsql-bugs@postgresql.org in case this is something that should be trapped in the engine, preventing a DB crash.

On Jun 16, 2016, at 7:04 PM, Robert Messer notifications@github.com wrote:

I have a function that crashes my postgres 9.5 server. Here is the PL/R function:

CREATE OR REPLACE FUNCTION test_svg () RETURNS text AS $$ library("svglite") library("ggplot2")

set up to output svg to a string

s = svgstring()

create the plot, as an example

ggplot(mapping = aes(cars$speed)) + geom_density()

NOTE: this works:

plot(density(cars$speed))

fetch the string

svg = s()

turn off output to the string

dev.off()

return(svg) $$ LANGUAGE 'plr' IMMUTABLE; When I run this, the whole server crashes, and all connection are dropped. Here is the main part of the error message in my postgresql logs:

Error: CHAR() can only be applied to a 'CHARSXP', not a 'pairlist' * stack smashing detected *: postgres: postgres c7_70 [local] SELECT terminated ======= Backtrace: ========= /lib64/libc.so.6(__fortify_fail+0x37)[0x7fc78c616b37] /lib64/libc.so.6(__fortify_fail+0x0)[0x7fc78c616b00] /usr/lib64/R/lib/libR.so(+0x1412d1)[0x7fc73f2182d1]

Curiously, if I simply remove the return statement and do return("foo"), it works fine (but of course I don't get the output I want. So somehow the process of returning the svg and coercing to the postgresql text causes a hard crash. It is also odd to me that the regular old "plot" works just fine. Maybe that means this is a bug in svglite or ggplot2, but it would be nice if PL/R prevented against server crashes as much as possible. Maybe it is something about the svg generated by ggplot2 that causes the problem.

As a side note, I am new to PL/R but find it confusing how returning text often fails. For example, if I have two string and concatenate them, no dice. That is, this doesn't work:

CREATE OR REPLACE FUNCTION R_test_cat () RETURNS text AS $$ return(cat("foo","bar")) $$ language 'plr'; When running this I get nothing, whereas a simple return("foo bar") works just fine. If there is something I am going wrong in either of these cases please advise. Thanks, and appreciate the efforts on PL/R, it is a very useful modules.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jconway/plr/issues/22#issuecomment-226659219, or mute the thread https://github.com/notifications/unsubscribe/AAYz9pASxm0ypfkOw1NS8R7w_twQyE2jks5qMfk1gaJpZM4I35LI .